import {
	defineStore
} from 'pinia'

import useFriend from './friend'
import useUser from './user'
import $S from '@/server/server.js'

const useOption = defineStore('option', {
	state: () => {
		return {
			drawerList: [{ // 点击加号弹层的选项
					id: 1,
					icon: '\ue68d',
					text: '发起群聊'
				},
				{
					id: 2,
					icon: '\ue8cd',
					text: '添加朋友'
				},
				{
					id: 3,
					icon: '\ue707',
					text: '扫一扫'
				},
				{
					id: 4,
					icon: '\ue619',
					text: '收付款'
				},
			],
			addressDefaultList: { // 通讯录页面的默认选项列表
				initial: '⬆',
				list: [{
						id: 1,
						cover: '/static/friend/friend.png',
						title: '新的朋友',
						options: true
					},
					{
						id: 2,
						cover: '/static/friend/group.png',
						title: '群聊',
						options: true
					}
				]
			},
			friendOptionsList: [{ // 可操作的朋友选项
					id: 1,
					title: '备注标签',
					path: '/pages/remarks/remarks'
				},
				{
					id: 2,
					title: '朋友权限',
					path: '/pages/permission/permission'
				},
				{
					id: 3,
					title: '朋友圈',
					path: 'page'
				},
				{
					id: 4,
					title: '投诉',
					path: '/pages/complain/complain'
				},
				{
					id: 5,
					title: '星标好友',
					path: false,
					checked: false,
					switch: true
				},
				{
					id: 7,
					title: '加入黑名单',
					path: false,
					checked: true,
					switch: true
				}
			],
			chatMoreOption: [ // 更多操作的数据
				{
					text: '语音',
					scr: '/static/more/audio.png'
				},
				{
					text: '发红包',
					scr: '/static/more/hongbao.png'
				},
				{
					text: '名片',
					scr: '/static/more/man.png'
				},
				{
					text: '定位',
					scr: '/static/more/path.png'
				},
				{
					text: '打电话',
					scr: '/static/more/phone.png'
				},
				{
					text: '照片',
					scr: '/static/more/pic.png'
				},
				{
					text: '收藏',
					scr: '/static/more/shoucan.png'
				},
				{
					text: '视频',
					scr: '/static/more/video.png'
				}
			],
			emoList: [ // 表情列表
				{
					id:0,
					url: '/static/emo/0.gif'
				},
				{
					id:1,
					url: '/static/emo/1.gif'
				},
				{
					id:2,
					url: '/static/emo/2.gif'
				},
				{
					id:3,
					url: '/static/emo/3.gif'
				},
				{
					id:4,
					url: '/static/emo/4.gif'
				},
				{
					id:5,
					url: '/static/emo/5.gif'
				},
				{
					id:6,
					url: '/static/emo/6.gif'
				},
				{
					id:7,
					url: '/static/emo/7.gif'
				},
				{
					id:8,
					url: '/static/emo/8.gif'
				},
				{
					id:9,
					url: '/static/emo/9.gif'
				},
				{
					id:10,
					url: '/static/emo/10.gif'
				},
				{
					id:11,
					url: '/static/emo/11.gif'
				},
				{
					id:12,
					url: '/static/emo/12.gif'
				},
				{
					id:13,
					url: '/static/emo/13.gif'
				},
				{
					id:14,
					url: '/static/emo/14.gif'
				},
				{
					id:15,
					url: '/static/emo/15.gif'
				},
				{
					id:16,
					url: '/static/emo/16.gif'
				},
				{
					id:17,
					url: '/static/emo/17.gif'
				},
				{
					id:18,
					url: '/static/emo/18.gif'
				},
				{
					id:19,
					url: '/static/emo/19.gif'
				}
			],
			groupOptions: [ // 群的相关选项
				{
					title: '群聊名称'
				},
				{
					title: '群二维码'
				},
				{
					title: '群头像'
				},
				{
					title: '群内公告'
				},
				{
					title: '我的群昵称'
				},
				{
					title: '投诉'
				},
			],
			chatMessageOptions:[
				{
					title: '复制'
				},
				{
					title: '收藏'
				},
				{
					title: '转发'
				},
				{
					title: '删除'
				}
			]
			
		}
	},
	actions: {
		navigatorOption(id) {
			switch (id) {
				case 1:
					uni.navigateTo({
						url: '/pages/creategroup/creategroup'
					})
					break;
				case 2:
					uni.navigateTo({
						url: '/pages/search/search?from=addFriend'
					})
					break;
				case 3:
					// console.log(3)
					const friendStore = useFriend()
					uni.scanCode({
						autoZoom:false,
						success: function (res) {
							console.log(res);
							let data = JSON.parse(res.result)
							if(!data.type){
								return uni.showToast({
									title: '识别失败'
								})
							}
							if(data.type === 'user'){ // 扫描的是个人二维码
								const fdis = []
								console.log(friendStore.friendList);
								friendStore.friendList.forEach(f => {
									f.list.forEach(f2 => {
										fdis.push(f2.user_id)
									})
								})
								const flag = fdis.includes(data.user_id)
								if(flag){ // 去聊天页面
									$S.get(`/getUserInfo/${data.user_id}`,{},{token:true}).then(res1 => {
										res1.chat_type = 'user'
										uni.navigateTo({
											url:'/pages/chat/chat?info=' + encodeURIComponent(JSON.stringify(res1))
										})
									})
								}else{ // 去添加好友页面
								$S.get(`/getUserInfo/${data.user_id}`,{},{token:true}).then(res1 => {
									res1.chat_type = 'user'
									res1.name = res1.nickname ? res1.nickname : res1.username
									uni.navigateTo({
										url:'/pages/addFriend/addFriend?info=' + encodeURIComponent(JSON.stringify(res1))
									})
								})
								}
							}else{ // 群二维码
								let gids = []
								const userStore = useUser()
								$S.get(`/getGroupInfo/${data.id}`,{},{token:true}).then(res2 => {
									gids = res2.group_users.map(m => m.user_id)
									const flag = gids.includes(userStore.userinfo.id)
									let obj = {
										id: res2.id,
										nickname: res2.name,
										avatar: res2.avatar,
										chat_type: 'group',
										...res2
									}
									if(flag){ // 进入聊天页面
										uni.navigateTo({
											url:'/pages/chat/chat?info=' + encodeURIComponent(JSON.stringify(obj))
										})
									}else{ // 进入加入群聊页面
										uni.navigateTo({
											url:'/pages/addGroup/addGroup?info=' + encodeURIComponent(JSON.stringify(obj))
										})
									}
								})
							}
						}
					});
					break;
				default:
					console.log(4)
					break;
			}
			// console.log(id);
		}
	},
	getters: {

	}
})

export default useOption